using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Xml;

using PlugNT.Common;
using PlugNT.Cms.Custom;

namespace PlugNT.Cms.Custom
{



    public class AppPlugin
    {
        private static List<AppModel> _appList = new List<AppModel>();
        private static Dictionary<string, int> _appDictionary = new Dictionary<string,int>();

        public static List<AppModel> appList
        {
            get
            {
                return _appList;
            }
        }

        static AppPlugin()
        {
            if (File.Exists(WebConfig.ConfigPath + "plugin.config"))
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(WebConfig.ConfigPath + "plugin.config");
                XmlNodeList controls = xml.SelectNodes("/configs/plugitem");
                foreach (XmlNode control in controls)
                {
                    XmlNode assembly = control.SelectSingleNode("assembly");
                    XmlNode plug = control.SelectSingleNode("plug");
                    AppModel app = new AppModel();
                    app.name = plug.InnerText;
                    app.assVersion = plug.Attributes["assversion"].Value;
                    app.setuped = plug.Attributes["setuped"].Value.ToLower() == "true";
                    app.assemblyName = control.Attributes["name"].Value;
                    XmlNode sysmodelid = assembly.SelectSingleNode("sysmodelid");
                    if (sysmodelid != null)
                    {
                        app.sysModelId = PlugNT.Common.Utils.IsNumeric(sysmodelid.InnerText) ? int.Parse(sysmodelid.InnerText) : 0;
                    }
                    XmlNode article = assembly.SelectSingleNode("article");
                    if (article != null)
                    {
                        app.className = article.Attributes["class"].Value;
                        XmlNode method = assembly.SelectSingleNode("article/method");
                        if (method != null)
                        {
                            app.artAddMethod = method.Attributes["add"].Value;
                            app.artUptMethod = method.Attributes["upt"].Value;
                            app.artDelMethod = method.Attributes["del"].Value;
                        }
                    }
                    XmlNode leftmenu = assembly.SelectSingleNode("ucenter/leftmenu");
                    if (leftmenu != null)
                    {
                        app.ucLeftmenu = leftmenu.InnerText;
                    }

                    _appList.Add(app);
                }
                int tCount=_appList.Count;
                for (int i = 0; i < tCount; i++)
                {
                    _appDictionary.Add(_appList[i].assemblyName, i);
                }
            }
        }

        public static AppModel GetAppModel(string key)
        {
            int aIndex = 0;
            if (_appDictionary.TryGetValue(key, out aIndex))
            {
                return _appList[aIndex];
            }
            else
            {
                return null;
            }
        }

    }


}

